package com.paic.qhcs.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.paic.qhcs.util.entry.Header;
import com.paic.qhcs.util.entry.MessageEntry;
import com.paic.qhcs.util.entry.ResultEntry;
import com.paic.qhcs.util.entry.SecurityInfo;

import net.sf.json.JSONObject;

/**
 * 请求示例参考
 * 
 * @author 唐应泉
 * @support by Tel:0755-22625539
 * @company 深圳前海征信中心股份有限公司
 * 
 */

public class DemoMain
{
    public static void main(String[] args) throws Exception
    {

    }
    
    
    /**
     * 发送HTTP请求
     * 
     * @throws Exception
     */
    private static void postHttpRequest() throws Exception
    {
        String sfUrl = "http://test-qhzx.pingan.com.cn:5443";
        String header = "\"header\":" + MessageUtil.getMHeader();
        String encBusiData = DataSecurityUtil.encrypt(MessageUtil.getBusiData_MSC8004().getBytes(),
                "SK803@!QLF-D25WEDA5E52DA");
        String busiData = "\"busiData\":\"" + encBusiData + "\"";
        String sigValue = DataSecurityUtil.signData(encBusiData);
        String pwd = DataSecurityUtil.digest("weblogic1".getBytes());
        String securityInfo = "\"securityInfo\":" + MessageUtil.getSecurityInfo(sigValue, pwd);
        String message = "{" + header + "," + busiData + "," + securityInfo + "}";
        System.out.println("请求：" + message);

        String res = HttpRequestUtil.sendJsonWithHttp(sfUrl, message);

        System.out.println("响应Message：" + res);
        JSONObject msgJSON = net.sf.json.JSONObject.fromObject(res);
        System.out.println("响应BusiData密文：" + msgJSON.getString("busiData"));

        // 一定要验证签名的合法性！！！！！！！！
        DataSecurityUtil.verifyData(msgJSON.getString("busiData"), msgJSON.getJSONObject("securityInfo").getString(
                "signatureValue"));

        System.out.println("响应BusiData明文："+ DataSecurityUtil.decrypt(msgJSON.getString("busiData"), "SK803@!QLF-D25WEDA5E52DA"));
    }
    
    /**
     * 发送HTTPs请求,注意这里我们信任了任何服务器证书
     * 
     * @throws Exception
     */
    private static void postHttpsRequest() throws Exception
    {
        String sfUrl = "https://qhzx-dcs.pingan.com.cn/do/dmz/query/credoo/v1/MSC8005";
    	//String sfUrl = "https://test-qhzx.pingan.com.cn:5443/do/dmz/query/credoo/v1/ MSC8005";
        String header = "\"header\":" + MessageUtil.getMHeader_DMZ("50178073-8", "huodui", "transNo"+System.currentTimeMillis()%1000000, null, "THR0093XX", null);
        String encBusiData = DataSecurityUtil.encrypt(MessageUtil.getBusiData("batNo"+System.currentTimeMillis()%1000, "01", "370283198711174587", "0", "宿彦慧", "01").getBytes(),
                "M~k\\rj4q8U`pdWjYaX7E7954");
        String busiData = "\"busiData\":\"" + encBusiData + "\"";
        String sigValue = DataSecurityUtil.signData(encBusiData);
        String pwd = DataSecurityUtil.digest("QS!,!828".getBytes());
        String securityInfo = "\"securityInfo\":" + MessageUtil.getSecurityInfo(sigValue, pwd);
        String message = "{" + header + "," + busiData + "," + securityInfo + "}";
        System.out.println("请求：" + message);

        String res = HttpRequestUtil.sendJsonWithHttps(sfUrl, message);

        System.out.println("响应Message：" + res);
        JSONObject msgJSON = JSONObject.fromObject(res);
        System.out.println("响应BusiData密文：" + msgJSON.getString("busiData"));

        // 一定要验证签名的合法性！！！！！！！！
        try{
        DataSecurityUtil.verifyData(msgJSON.getString("busiData"), msgJSON.getJSONObject("securityInfo").getString(
                "signatureValue"));
        }catch(Exception e){
        	e.printStackTrace();
        }

        System.out.println("响应BusiData明文："
                + DataSecurityUtil.decrypt(msgJSON.getString("busiData"), "M~k\\rj4q8U`pdWjYaX7E7954"));
    }
}
